package com.dmm.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dmm.Exception.Exceptionbean;
import com.dmm.redis.redisService;
import com.dmm.user.entity.Signin;
import com.dmm.user.entity.Userinfo;
import com.dmm.user.mapper.SigninMapper;
import com.dmm.user.service.SigninService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dmm.user.service.UserinfoService;
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Random;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dmm
 * @since 2023-03-27
 */
@Service
public class SigninServiceImpl extends ServiceImpl<SigninMapper, Signin> implements SigninService {

    @Resource
    SigninMapper signinMapper;

    @Resource
    UserinfoService userinfoService;

    @Autowired
    redisService redisService;

    //发送验证码
    @Override
    public void EmailCode(String email) {

        String str= "";

        for(int i=0;i<6;i++){
            str=str+new Random().nextInt(10);
        }


        System.out.println(str);

//        将验证码存入redis
        redisService.set2(email,str,1);

        // 发送验证码到邮箱
        Email email1 = new SimpleEmail();


        try {
            email1.setHostName("smtp.qq.com");
            //端口号
            email1.setSmtpPort(465);

            email1.setCharset("utf-8");//设置发送的字符类型
            email1.addTo(email);//设置收件人
            email1.setFrom("1458119489@qq.com");//发送人的邮箱为自己的，用户名可以随便填
            //设置发送人到的邮箱和用户名和授权码(授权码是自己设置的)
            email1.setAuthenticator(new DefaultAuthenticator("1458119489@qq.com","jwwyvnzbgernfjha"));

            email1.setSSLOnConnect(true);

            email1.setSubject("注册验证码");//设置发送主题
            email1.setMsg("验证码有效时间1分钟"+str);//设置发送内容
            email1.send();//进行发送

        } catch (EmailException e) {
            e.printStackTrace();
        }


    }


    //注册
    @Override
    public void registeremail(Signin signin) {
        int i=0;
        //校验code
        String s = redisService.get(signin.getEmail());
        String code = String.valueOf(signin.getCode());

        code='"'+code+'"';
        if(StringUtils.isEmpty(s)){
            throw new Exceptionbean(20001,"验证码不存在或者已经过期");
        }else{
            if(s.equals(code)){
                i=1;
            }
        }

        //校验失败
        if(i==0){
            throw new Exceptionbean(20001,"验证码错误");
        }
        //查询账号是否存在
        QueryWrapper<Signin> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq( "email",signin.getEmail());
        Signin signin1 = signinMapper.selectOne(queryWrapper);

        try {
            System.out.println(signin1.toString());
        }catch (Exception e){
            //添加
            signinMapper.insert(signin);
            boolean b = userinfoService.saveUserinfo(signin.getEmail());

            return;
        }


        throw new Exceptionbean(20001,"账号已存在");


    }
}
